package com.bankcomm.creditcard.redfriday.map.baidu;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.bankcomm.creditcard.redfriday.MyApplication;
import com.bankcomm.creditcard.redfriday.R;
import com.bankcomm.creditcard.redfriday.data.GPS;
import com.bankcomm.creditcard.redfriday.data.Shop;

public class BaiduMapActivity extends MapActivity {

	private static final String TAG = "BaiduMapActivity";
	private View mPopView = null; // 点击mark时弹出的气泡View
	private MapView mMapView = null;
	private MyApplication myApplication = null;
	private BMapManager mBMapManager = null;
	private List<Shop> mList = new ArrayList<Shop>();
	private boolean mIsToMyPoi = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_baidu);

		ArrayList<Bundle> list = getIntent().getParcelableArrayListExtra(
				"shops");
		if (list == null) {
			list = new ArrayList<Bundle>();
		}
		for (Iterator<Bundle> iterator = list.iterator(); iterator.hasNext();) {
			Bundle bundle = iterator.next();
			Shop shop = (Shop) bundle.getSerializable("shop");
			mList.add(shop);
		}

		String from = getIntent().getStringExtra("from");
		boolean isAniToI = false;
		if (from.equals("shoplist")) {
			isAniToI = true;
		}

		int zoom = 15;
		int distance = getIntent().getIntExtra("distance", 1);
		switch (distance) {
		case 1:
			zoom = 16;
			break;
		case 3:
			zoom = 13;
			break;
		case 5:
			zoom = 10;
			break;
		case 10:
			zoom = 8;
			break;
		default:
			zoom = 15;
			break;
		}

		TextView titleTextView = (TextView) findViewById(R.id.header_title);
		titleTextView.setText(R.string.map);

		Button backBtn = (Button) findViewById(R.id.header_back);
		backBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		myApplication = (MyApplication) getApplication();
		mBMapManager = myApplication.getBMapManager();

		mBMapManager.start();
		// 如果使用地图SDK，请初始化地图Activity
		initMapActivity(mBMapManager);

		mMapView = (MapView) findViewById(R.id.mapview_baidu);
		mMapView.setBuiltInZoomControls(true);
		// 设置在缩放动画过程中也显示overlay,默认为不绘制
		mMapView.setDrawOverlayWhenZooming(true);
		GeoPoint myGeoPoint = new GeoPoint((int) (GPS.LAT * 1e6),
				(int) (GPS.LNG * 1e6));

		// 添加ItemizedOverlay
		Drawable myMarker = getResources().getDrawable(R.drawable.i_poi);
		// 得到需要标在地图上的资源
		myMarker.setBounds(0, 0, myMarker.getIntrinsicWidth(),
				myMarker.getIntrinsicHeight()); // 为maker定义位置和边界

		CustomItemizedOverlay myItemizedOverlay = new CustomItemizedOverlay(
				myMarker, this);
		myItemizedOverlay.addOverlay(new OverlayItem(myGeoPoint, "", ""));
		mMapView.getOverlays().add(myItemizedOverlay); // 添加ItemizedOverlay实例到mMapView

		GeoPoint shopGeoPoint = null;
		if (!mList.isEmpty()) {
			Drawable shopMarker = getResources().getDrawable(R.drawable.poi); //
			// 得到需要标在地图上的资源
			shopMarker.setBounds(0, 0, shopMarker.getIntrinsicWidth(),
					shopMarker.getIntrinsicHeight()); // 为maker定义位置和边界
			CustomItemizedOverlay shopItemizedOverlay = new CustomItemizedOverlay(
					shopMarker, this);
			for (Iterator<Shop> iterator = mList.iterator(); iterator.hasNext();) {
				Shop shop = iterator.next();
				GeoPoint geoPoint = new GeoPoint((int) (shop.lat * 1e6),
						(int) (shop.lng * 1e6));
				shopGeoPoint = geoPoint;
				shopItemizedOverlay.addOverlay(new OverlayItem(geoPoint,
						shop.marketTitle, ""));
			}
			mMapView.getOverlays().add(shopItemizedOverlay);
		}
		// 创建点击mark时的弹出泡泡
		// mPopView = super.getLayoutInflater().inflate(R.layout.popview, null);
		// mMapView.addView(mPopView, new MapView.LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null,
		// MapView.LayoutParams.TOP_LEFT));
		// mPopView.setVisibility(View.GONE);

		final MapController mapController = mMapView.getController();
		mapController.setZoom(16);

		if (from.equals("shop")) {
			final Button poiBtn = (Button) findViewById(R.id.header_home);
			poiBtn.setVisibility(View.VISIBLE);
			poiBtn.setText(getString(R.string.map_mypoi));
			mIsToMyPoi = true;
			final GeoPoint finalMyGeoPoint = myGeoPoint;
			final GeoPoint finalShopGeoPoint = shopGeoPoint;
			poiBtn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (mIsToMyPoi) {
						mapController.setCenter(finalMyGeoPoint);
						mapController.animateTo(finalMyGeoPoint);
						poiBtn.setText(getString(R.string.map_shoppoi));
						mIsToMyPoi = false;
					} else {
						if (finalShopGeoPoint != null) {
							mapController.setCenter(finalShopGeoPoint);
							mapController.animateTo(finalShopGeoPoint);
							poiBtn.setText(getString(R.string.map_mypoi));
							mIsToMyPoi = true;
						}
					}
				}
			});
		}

		if (isAniToI) {
			mapController.setCenter(myGeoPoint);
			mapController.animateTo(myGeoPoint);
		} else {
			if (shopGeoPoint != null) {
				mapController.setCenter(shopGeoPoint);
				mapController.animateTo(shopGeoPoint);
			}
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		// mBMapManager.stop();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// mBMapManager.start();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
